/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.bld.model.municipio;

import br.com.bld.util.facade.AbstractDao;
import br.com.bld.util.facade.EntityFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

/**
 *
 * @author Pedro
 */
class MunicipioDao extends AbstractDao {

    public MunicipioDao(Connection conn, EntityFactory entityFactory) {

        super(conn, entityFactory);
    }

    private MunicipioPK getPk(ResultSet rs) throws SQLException {

        int i = 0;

        MunicipioPK resposta = new MunicipioPK(rs.getLong(++i));

        return resposta;
    }

    private MunicipioEntity getEntity(ResultSet rs) throws SQLException {
        MunicipioEntity resposta = new MunicipioEntity();

        int i = 1;

        resposta.setNome(rs.getString(++i));
        resposta.setUf(rs.getString(++i));

        return resposta;
    }

    public int insert(MunicipioEntity entity, MunicipioPK pk) throws SQLException {

        String sql = createInsert();

        PreparedStatement ps = null;

        try {

            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setLong(++i, pk.getId());
            ps.setString(++i, entity.getNome());
            ps.setString(++i, entity.getUf());

            return executeUpdate(ps);

        } finally {
            close(ps);
        }
    }

    public int update(MunicipioEntity entity, MunicipioPK pk) throws SQLException {
        String sql = createUpdate();

        PreparedStatement ps = null;

        try {

            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setString(++i, entity.getNome());
            ps.setString(++i, entity.getUf());

            ps.setLong(++i, pk.getId());

            return executeUpdate(ps);

        } finally {
            close(ps);
        }

    }

    public int delete(MunicipioPK pk) throws SQLException {

        String sql = createDelete();

        PreparedStatement ps = null;

        try {

            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setLong(++i, pk.getId());

            return executeUpdate(ps);

        } finally {
            close(ps);
        }
    }

    public HashMap<MunicipioPK, MunicipioEntity> selectAll() throws SQLException {

        HashMap<MunicipioPK, MunicipioEntity> resposta = new HashMap();
        String sql = createSelect();

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {

            ps = getPreparedStatement(sql);
            rs = getResultSet(ps);

            while (rs.next()) {
                resposta.put(getPk(rs), getEntity(rs));
            }

            return resposta;
        } finally {
            close(ps, rs);
        }
    }

    public MunicipioEntity selectByPk(MunicipioPK pk) throws SQLException {
        MunicipioEntity resposta = null;

        String sql = createSelectByPk();

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {

            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setLong(++i, pk.getId());

            rs = getResultSet(ps);

            if (rs.next()) {
                resposta = getEntity(rs);
            }

            return resposta;
        } finally {
            close(ps, rs);
        }
    }

    public HashMap<MunicipioPK, MunicipioEntity> selectByPage(Integer numrPagina, Integer qtdePorPagina) throws SQLException {

        HashMap<MunicipioPK, MunicipioEntity> resposta = new HashMap();
        String sql = createSelectByPage(numrPagina, qtdePorPagina);

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {

            ps = getPreparedStatement(sql);
            rs = getResultSet(ps);

            while (rs.next()) {
                resposta.put(getPk(rs), getEntity(rs));
            }

            return resposta;
        } finally {
            close(ps, rs);
        }
    }

    public Collection<String> selectUF() throws SQLException {

        String[] key = {"uf"};

        StringBuilder sql = new StringBuilder();
        
        sql.append(createSelectDistinct(key)).append(createOrderByKeyAsc(key));

        Collection<String> resposta = new ArrayList<>();

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            ps = getPreparedStatement(sql.toString());
            rs = getResultSet(ps);

            while (rs.next()) {
                resposta.add(rs.getString(1));
            }

            return resposta;

        } finally {
            close(ps, rs);
        }
    }
    
    public HashMap<MunicipioPK, MunicipioEntity> selectByUF(String uf) throws SQLException {
        String [] key = {"uf"};
        
        StringBuilder sql = new StringBuilder();
        sql.append(createSelectByKey(key)).append('\n').append(createOrderByKeyAsc(key));
        
        HashMap<MunicipioPK, MunicipioEntity> resposta = new HashMap<>();
        
        PreparedStatement ps = null;       
        ResultSet rs = null;
        
        try {
            
            ps = getPreparedStatement(sql.toString());
            
            ps.setString(1, uf);
            
            rs = getResultSet(ps);
            
            while(rs.next()) {
                resposta.put(getPk(rs), getEntity(rs));
            }
            
            return resposta;
            
        } finally {
            close(ps, rs);
        }
        
    }
}
